// pages/history/history.js
const app = getApp()
var util =  require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    historyList : '',
    btnTxt:{
      btnTxt: '选择对比'
    },
    btnTxt2:{
      btnTxt2: '删除'
    },
    dataCheck: false,
    checked:false,
    idList:{
      id1:  '',
      id2: '',
      time1: '',
      time2: ''
    },
    haveData: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(options.item != null && options.item != undefined)
    {
      that.setData({
        item: JSON.parse(options.item)
      })
      wx.setNavigationBarTitle({
        title: that.data.item.nickName + '的测量历史' //页面标题为路由参数
      })
      that.getHistoryList(that.data.item.userId);
    }
    else{
      that.getHistoryList();
    }
  },

  getHistoryList(userId){
    var that = this
    var id = ''
    if(userId != '' && userId != 'undefined' && userId != null){
      id = userId
    }else{
      id = app.globalData.userId
    }
    // console.log(app.globalData.userId)
    // console.log(id)
    wx.request({
      url: app.globalData.apiUrl +'/bodycheck',
      data: {
        id: id,
      },
      method: 'post',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data.code == '200'){
          for(var i in res.data.data)
          {
            var time = util.setTime(res.data.data[i].time)
            res.data.data[i].time = time;
          }
          that.setData({
            historyList : res.data.data
          })
        }
        else{
          that.setData({
            haveData: 2,
            tipTxt: {
              tipTxt: '暂无测量数据',
              tipImg: '/resources/index-image/noData.png'
            }
          })
        }
      },
      fail: function (res) {
        console.log("失败");
      }
    })
  },

  //点击列表
  analysis: function (e) {
    var that = this;
    if(that.data.dataCheck == false)
    {
      wx.navigateTo({
        url: '../analysis/analysis?id='+ e.currentTarget.dataset.id + '&item=' + JSON.stringify(that.data.item)
      }) 
    }
    else 
    {
      //选择对比
      // console.log(e)
      var id = e.currentTarget.dataset.id;
      var arr = that.data.historyList;
      var arr2 = [];
      var createtime = e.currentTarget.dataset.createtime
      var time = util.setTime(e.currentTarget.dataset.time);
      if(that.data.contrast == true){
        for(let i=0; i<arr.length;i++)
        {
          if(arr[i].time == time)
          {
            arr2.push(arr[i].list[0])
          } 
        }
        for(let j=0;j<arr2.length;j++)
        {
          for(let z=0;z<arr2[j].length;z++)
          {
            if(id == arr2[j][z]._id)
            {
              //判断选中项是否已取值
              if(arr2[j][z].check == true)
              {
                if(arr2[j][z]._id == that.data.idList.id1)
                {
                  var id1 = "idList.id1"
                  var time1 = "idList.time1"
                  that.setData({
                    [id1]:'',
                    [time1]: ''
                  })
                  // console.log(that.data.idList);
                }
                if(arr2[j][z]._id == that.data.idList.id2)
                {
                  var id2 = "idList.id2"
                  var time2 = "idList.time2"
                  that.setData({
                    [id2]:'',
                    [time2]: ''
                  })
                  // console.log(that.data.idList);
                }
              }
              else
              {
                if((that.data.idList.id1 == '' && that.data.idList.time1 == '') || (that.data.idList.id2 == '' && that.data.idList.time2 == ''))
                {
                  if(that.data.idList.id1 =='' ){
                    var id1 = "idList.id1"
                    var time1 = "idList.time1"
                    that.setData({
                      [id1]: id,
                      [time1]: createtime
                    })
                    // console.log(that.data.idList);
                  }
                  else{
                    var id2 = "idList.id2"
                    var time2 = "idList.time2"
                    that.setData({
                      [id2]: id,
                      [time2]: createtime
                    })
                    // console.log(that.data.idList);
                  }
                }
                else{
                  wx.showToast({
                    title: '只能选择两条记录进行对比',
                    icon: 'success',
                  })
                  setTimeout(function () {
                    wx.hideToast()
                  }, 2000)
                  break;
                }
              }
              arr2[j][z].check = !arr2[j][z].check;
              break
            }
          }
        }
        
      }
      else if(that.data.delete == true){
        //删除
        for(let i=0; i<arr.length;i++)
        {
          if(arr[i].time == time)
          {
            arr2.push(arr[i].list[0])
          } 
        }
        for(let j=0;j<arr2.length;j++)
        {
          for(let z=0;z<arr2[j].length;z++)
          {
            if(id == arr2[j][z]._id)
            {
              arr2[j][z].check = !arr2[j][z].check;
              break
            }
          }
        }
      }
      this.setData({
        historyList : arr
      })
    }
  },
  
  //选择按钮
  lickBtn: function () { 
    this.setData({
      dataCheck: true,
      contrast: true,
      delete: false
    })
  },
  
  //取消按钮
  cancelBtn:function(e) {
    var that = this
    that.setData({
      dataCheck: false,
    })
    that.cancelChoose()
  },

  //对比按钮
  contrastBtn:function() {
    var that = this
    if(that.data.idList.id1 != '' && that.data.idList.time1 != '' && that.data.idList.id2 != '' && that.data.idList.time2 != '')
    {
      wx.navigateTo({
        url: '../dataContrast/dataContrast?idList='+ JSON.stringify(that.data.idList) + '&item=' + JSON.stringify(that.data.item)
      }) 
    }
    else
    {
      wx.showToast({
        title: '请选择两条记录进行对比',
        icon: 'success',
      })
      setTimeout(function () {
        wx.hideToast()
      }, 2000)
    }
  },

  //取消选择
  cancelChoose:function(){
    var that = this
    // if(that.data.idList.id1 == '' && that.data.idList.time1 == '' && that.data.idList.id2 == '' && that.data.idList.time2 == '') return
    var arr = that.data.historyList;
    for(var i=0; i<arr.length;i++)
    {
      for(var j=0; j<arr[i].list[0].length;j++)
      {
        // console.log(arr[i].list[0][j])
        if(arr[i].list[0][j].check)
        {
          arr[i].list[0][j].check = !arr[i].list[0][j].check
        }
      }
    }
    var id1 = "idList.id1"
    var id2 = "idList.id2"
    var time1 = "idList.time1"
    var time2 = "idList.time2"
    that.setData({
      historyList : arr,
      [id1]: '',
      [id2]: '',
      [time1]: '',
      [time2]: '',
      dataCheck: false,
    })
  },

  //删除按钮
  lickBtn2:function(){
    this.setData({
      dataCheck: true,
      delete: true,
      contrast: false,
    })
  },

  //删除
  deleteBtn:function(){
    var that = this
    var arr = that.data.historyList;
    var id = ""
    for(var i=0; i<arr.length;i++)
    {
      for(var j=0; j<arr[i].list[0].length;j++)
      {
        // console.log(arr[i].list[0][j])
        if(arr[i].list[0][j].check)
          {
            id = arr[i].list[0][j]._id + ',' + id
          }
      }
    }
    if(id != '')
    {
      id = (id.substring(id.length-1) == ',') ? id.substring(0,id.length-1) : id;
      // console.log(id)
      wx.request({
        url: app.globalData.apiUrl +'/bodycheck',
        data: {
          id: id,
        },
        method: 'delete',
        header: {
          'content-type': 'application/json' //默认值
        },
        success: function (res) {
          // console.log(res)
          if(res.data.code == '200'){
            if(that.data.item != null && that.data.item != undefined){
              that.getHistoryList(that.data.item.userId)
            }else{
              that.getHistoryList();
            }
          }
          else{
            util.showModal('提示','数据删除失败')
            return;
          }
        },
        fail: function (res) {
          console.log("失败");
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.cancelChoose()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.cancelChoose()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: 'PAPN小程序',
      path: 'pages/index/index?puid=' + app.globalData.userId,
      success: function(res) {
          res.shareTickets 
      },
      fail: function(res) {
          console.log(res)
      }
    }
  }
})